import http from './http.js'
import { authApi } from './auth.js'
import { userApi } from './user.js'
import { documentApi } from './document.js'
import chatApi from './chat.js'
import { favoritesApi } from './favorites.js'
import { systemApi } from './system.js'

export {
  http,
  authApi,
  userApi,
  documentApi,
  chatApi,
  favoritesApi,
  systemApi
}

export default {
  http,
  auth: authApi,
  user: userApi,
  document: documentApi,
  chat: chatApi,
  favorites: favoritesApi,
  system: systemApi
}

// 工具函数
export const utils = {
	// 格式化文件大小
	formatFileSize: (bytes) => {
		if (bytes === 0) return '0 B'
		const k = 1024
		const sizes = ['B', 'KB', 'MB', 'GB', 'TB']
		const i = Math.floor(Math.log(bytes) / Math.log(k))
		return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]
	},
	
	// 格式化日期
	formatDate: (date, format = 'YYYY-MM-DD HH:mm:ss') => {
		const d = new Date(date)
		const year = d.getFullYear()
		const month = String(d.getMonth() + 1).padStart(2, '0')
		const day = String(d.getDate()).padStart(2, '0')
		const hours = String(d.getHours()).padStart(2, '0')
		const minutes = String(d.getMinutes()).padStart(2, '0')
		const seconds = String(d.getSeconds()).padStart(2, '0')
		
		return format
			.replace('YYYY', year)
			.replace('MM', month)
			.replace('DD', day)
			.replace('HH', hours)
			.replace('mm', minutes)
			.replace('ss', seconds)
	},
	
	// 防抖函数
	debounce: (func, wait) => {
		let timeout
		return function executedFunction(...args) {
			const later = () => {
				clearTimeout(timeout)
				func(...args)
			}
			clearTimeout(timeout)
			timeout = setTimeout(later, wait)
		}
	},
	
	// 显示loading
	showLoading: (title = '加载中...') => {
		uni.showLoading({
			title,
			mask: true
		})
	},
	
	// 隐藏loading
	hideLoading: () => {
		uni.hideLoading()
	},
	
	// 显示成功提示
	showSuccess: (title) => {
		uni.showToast({
			title,
			icon: 'success'
		})
	},
	
	// 显示错误提示
	showError: (title) => {
		uni.showToast({
			title,
			icon: 'error'
		})
	}
}
